<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-type" content="text/html; charset=iso-8859-1">
<title>Invoking Other Web Resources - The Java EE 5 Tutorial</title>
<meta name="robots" content="index,follow">
<meta name="robots" content="index,follow">
<meta name="date" content="2008-10-01">
<link rel="stylesheet" type="text/css" href="css/default.css">
<link rel="stylesheet" type="text/css" href="css/ipg.css">
<link rel="stylesheet" type="text/css" href="css/j5eetutorial.css">
</head>

<body>

<table border="0" cellpadding="5" cellspacing="0" width="100%">
<tbody>
   <tr valign="top">
      <td><p class="toc level1"><a href="docinfo.html">Document Information</a></p>
<p class="toc level1 tocsp"><a href="gexaf.html">Preface</a></p>
<p class="toc level1 tocsp"><a href="gfirp.html">Part&nbsp;I&nbsp;Introduction</a></p>
<p class="toc level2"><a href="bnaaw.html">1.&nbsp;&nbsp;Overview</a></p>
<p class="toc level2"><a href="gfiud.html">2.&nbsp;&nbsp;Using the Tutorial Examples</a></p>
<p class="toc level1 tocsp"><a href="bnadp.html">Part&nbsp;II&nbsp;The Web Tier</a></p>
<p class="toc level2"><a href="bnadr.html">3.&nbsp;&nbsp;Getting Started with Web Applications</a></p>
<p class="toc level2"><a href="bnafd.html">4.&nbsp;&nbsp;Java Servlet Technology</a></p>
<p class="toc level3"><a href="bnafe.html">What Is a Servlet?</a></p>
<p class="toc level3"><a href="bnaff.html">The Example Servlets</a></p>
<p class="toc level4"><a href="bnaff.html#bnafh">Troubleshooting Duke's Bookstore Database Problems</a></p>
<p class="toc level3 tocsp"><a href="bnafi.html">Servlet Life Cycle</a></p>
<p class="toc level4"><a href="bnafi.html#bnafj">Handling Servlet Life-Cycle Events</a></p>
<p class="toc level5"><a href="bnafi.html#bnafk">Defining the Listener Class</a></p>
<p class="toc level5"><a href="bnafi.html#bnafm">Specifying Event Listener Classes</a></p>
<p class="toc level4 tocsp"><a href="bnafi.html#bnafn">Handling Servlet Errors</a></p>
<p class="toc level3 tocsp"><a href="bnafo.html">Sharing Information</a></p>
<p class="toc level4"><a href="bnafo.html#bnafp">Using Scope Objects</a></p>
<p class="toc level4"><a href="bnafo.html#bnafs">Controlling Concurrent Access to Shared Resources</a></p>
<p class="toc level4"><a href="bnafo.html#bnaft">Accessing Databases</a></p>
<p class="toc level3 tocsp"><a href="bnafu.html">Initializing a Servlet</a></p>
<p class="toc level3"><a href="bnafv.html">Writing Service Methods</a></p>
<p class="toc level4"><a href="bnafv.html#bnafw">Getting Information from Requests</a></p>
<p class="toc level4"><a href="bnafv.html#bnafz">Constructing Responses</a></p>
<p class="toc level3 tocsp"><a href="bnagb.html">Filtering Requests and Responses</a></p>
<p class="toc level4"><a href="bnagb.html#bnagc">Programming Filters</a></p>
<p class="toc level4"><a href="bnagb.html#bnagd">Programming Customized Requests and Responses</a></p>
<p class="toc level4"><a href="bnagb.html#bnagf">Specifying Filter Mappings</a></p>
<div class="onpage">
<p class="toc level3 tocsp"><a href="">Invoking Other Web Resources</a></p>
<p class="toc level4"><a href="#bnagj">Including Other Resources in the Response</a></p>
<p class="toc level4"><a href="#bnagk">Transferring Control to Another Web Component</a></p>
</div>
<p class="toc level3 tocsp"><a href="bnagl.html">Accessing the Web Context</a></p>
<p class="toc level3"><a href="bnagm.html">Maintaining Client State</a></p>
<p class="toc level4"><a href="bnagm.html#bnagn">Accessing a Session</a></p>
<p class="toc level4"><a href="bnagm.html#bnago">Associating Objects with a Session</a></p>
<p class="toc level5"><a href="bnagm.html#bnagp">Notifying Objects That Are Associated with a Session</a></p>
<p class="toc level4 tocsp"><a href="bnagm.html#bnagq">Session Management</a></p>
<p class="toc level4"><a href="bnagm.html#bnagr">Session Tracking</a></p>
<p class="toc level3 tocsp"><a href="bnags.html">Finalizing a Servlet</a></p>
<p class="toc level4"><a href="bnags.html#bnagt">Tracking Service Requests</a></p>
<p class="toc level4"><a href="bnags.html#bnagu">Notifying Methods to Shut Down</a></p>
<p class="toc level4"><a href="bnags.html#bnagv">Creating Polite Long-Running Methods</a></p>
<p class="toc level3 tocsp"><a href="bnagw.html">Further Information about Java Servlet Technology</a></p>
<p class="toc level2 tocsp"><a href="bnagx.html">5.&nbsp;&nbsp;JavaServer Pages Technology</a></p>
<p class="toc level2"><a href="bnajo.html">6.&nbsp;&nbsp;JavaServer Pages Documents</a></p>
<p class="toc level2"><a href="bnakc.html">7.&nbsp;&nbsp;JavaServer Pages Standard Tag Library</a></p>
<p class="toc level2"><a href="bnalj.html">8.&nbsp;&nbsp;Custom Tags in JSP Pages</a></p>
<p class="toc level2"><a href="bnaon.html">9.&nbsp;&nbsp;Scripting in JSP Pages</a></p>
<p class="toc level2"><a href="bnaph.html">10.&nbsp;&nbsp;JavaServer Faces Technology</a></p>
<p class="toc level2"><a href="bnaqz.html">11.&nbsp;&nbsp;Using JavaServer Faces Technology in JSP Pages</a></p>
<p class="toc level2"><a href="bnatx.html">12.&nbsp;&nbsp;Developing with JavaServer Faces Technology</a></p>
<p class="toc level2"><a href="bnavg.html">13.&nbsp;&nbsp;Creating Custom UI Components</a></p>
<p class="toc level2"><a href="bnawo.html">14.&nbsp;&nbsp;Configuring JavaServer Faces Applications</a></p>
<p class="toc level2"><a href="bnaxu.html">15.&nbsp;&nbsp;Internationalizing and Localizing Web Applications</a></p>
<p class="toc level1 tocsp"><a href="bnayk.html">Part&nbsp;III&nbsp;Web Services</a></p>
<p class="toc level2"><a href="bnayl.html">16.&nbsp;&nbsp;Building Web Services with JAX-WS</a></p>
<p class="toc level2"><a href="bnazf.html">17.&nbsp;&nbsp;Binding between XML Schema and Java Classes</a></p>
<p class="toc level2"><a href="bnbdv.html">18.&nbsp;&nbsp;Streaming API for XML</a></p>
<p class="toc level2"><a href="bnbhf.html">19.&nbsp;&nbsp;SOAP with Attachments API for Java</a></p>
<p class="toc level1 tocsp"><a href="bnblr.html">Part&nbsp;IV&nbsp;Enterprise Beans</a></p>
<p class="toc level2"><a href="bnbls.html">20.&nbsp;&nbsp;Enterprise Beans</a></p>
<p class="toc level2"><a href="bnbnb.html">21.&nbsp;&nbsp;Getting Started with Enterprise Beans</a></p>
<p class="toc level2"><a href="bnboc.html">22.&nbsp;&nbsp;Session Bean Examples</a></p>
<p class="toc level2"><a href="bnbpk.html">23.&nbsp;&nbsp;A Message-Driven Bean Example</a></p>
<p class="toc level1 tocsp"><a href="bnbpy.html">Part&nbsp;V&nbsp;Persistence</a></p>
<p class="toc level2"><a href="bnbpz.html">24.&nbsp;&nbsp;Introduction to the Java Persistence API</a></p>
<p class="toc level2"><a href="bnbrl.html">25.&nbsp;&nbsp;Persistence in the Web Tier</a></p>
<p class="toc level2"><a href="bnbrs.html">26.&nbsp;&nbsp;Persistence in the EJB Tier</a></p>
<p class="toc level2"><a href="bnbtg.html">27.&nbsp;&nbsp;The Java Persistence Query Language</a></p>
<p class="toc level1 tocsp"><a href="bnbwi.html">Part&nbsp;VI&nbsp;Services</a></p>
<p class="toc level2"><a href="bnbwj.html">28.&nbsp;&nbsp;Introduction to Security in the Java EE Platform</a></p>
<p class="toc level2"><a href="bnbyk.html">29.&nbsp;&nbsp;Securing Java EE Applications</a></p>
<p class="toc level2"><a href="bncas.html">30.&nbsp;&nbsp;Securing Web Applications</a></p>
<p class="toc level2"><a href="bncdq.html">31.&nbsp;&nbsp;The Java Message Service API</a></p>
<p class="toc level2"><a href="bncgv.html">32.&nbsp;&nbsp;Java EE Examples Using the JMS API</a></p>
<p class="toc level2"><a href="bncih.html">33.&nbsp;&nbsp;Transactions</a></p>
<p class="toc level2"><a href="bncjh.html">34.&nbsp;&nbsp;Resource Connections</a></p>
<p class="toc level2"><a href="bncjx.html">35.&nbsp;&nbsp;Connector Architecture</a></p>
<p class="toc level1 tocsp"><a href="bnckn.html">Part&nbsp;VII&nbsp;Case Studies</a></p>
<p class="toc level2"><a href="bncko.html">36.&nbsp;&nbsp;The Coffee Break Application</a></p>
<p class="toc level2"><a href="bnclz.html">37.&nbsp;&nbsp;The Duke's Bank Application</a></p>
<p class="toc level1 tocsp"><a href="gexbq.html">Part&nbsp;VIII&nbsp;Appendixes</a></p>
<p class="toc level2"><a href="bncno.html">A.&nbsp;&nbsp;Java Encoding Schemes</a></p>
<p class="toc level2"><a href="bncnq.html">B.&nbsp;&nbsp;Preparation for Java EE Certification Exams</a></p>
<p class="toc level2"><a href="bncnt.html">C.&nbsp;&nbsp;About the Authors</a></p>
<p class="toc level1 tocsp"><a href="idx-1.html">Index</a></p>
</td>
      <td width="10px">&nbsp;</td>
      <td width="705px">
         <div class="header">
             <div class="header-links-top">
                 <a href="http://java.sun.com">java.sun.com</a> |
                 <a href="http://docs.sun.com/">docs.sun.com</a><br>
             </div> 
             <img src="graphics/tutorialBanner.gif" width="704" height="120" alt="The Java&trade; EE 5 Tutorial"/>
             <div class="header-links">
	         <a href="index.html">Home</a> |
                 <a href="http://java.sun.com/javaee/5/docs/tutorial/information/download.html">Download</a> |
                 <a href="http://java.sun.com/javaee/5/docs/tutorial/doc/JavaEETutorial.pdf">PDF</a> |
                 <a href="http://java.sun.com/javaee/5/docs/api/index.html">API</a> |
                 <a href="http://java.sun.com/javaee/5/docs/tutorial/information/faq.html">FAQ</a> |
                 <a href="http://java.sun.com/javaee/5/docs/tutorial/information/search.html">Search</a> |
                 <a href="http://java.sun.com/javaee/5/docs/tutorial/information/sendusmail.html">Feedback</a> |
                 <a href="http://java.sun.com/javaee/5/docs/tutorial/information/history.html">History</a>
             </div>
             <div class="navigation">
                 <a href="bnagb.html"><img style="padding-right: 3px" src="graphics/leftButton.gif" border="0"></a>
                 <a href="sjsaseej2eet.html"><img style="padding-right: 3px" src="graphics/upButton.gif" border="0"></a>
                 <a href="bnagl.html"><img style="padding-left: 3px" src="graphics/rightButton.gif" border="0"></a>
             </div>
         </div>

	 <div class="maincontent">      	 
             

<a name="bnagi"></a><h3>Invoking Other Web Resources</h3>
<p><a name="indexterm-282"></a>Web components can invoke other web resources in two ways: indirectly and directly.
A web component indirectly invokes another web resource when it embeds a URL
that points to another web component in content returned to a client. In
the Duke&rsquo;s Bookstore application, most web components contain embedded URLs that point to
other web components. For example, <tt>ShowCartServlet</tt> indirectly invokes the <tt>CatalogServlet</tt> through the following
embedded URL:</p><pre>/bookstore1/catalog</pre><p>A web component can also directly invoke another resource while it is executing.
There are two possibilities: The web component can include the content of another
resource, or it can forward a request to another resource.</p><p><a name="indexterm-283"></a><a name="indexterm-284"></a>To invoke a resource available on the server that is running a web
component, you must first obtain a <a href="http://java.sun.com/javaee/5/docs/api/javax/servlet/RequestDispatcher.html">RequestDispatcher</a> object using the <tt>getRequestDispatcher("URL")</tt> method.</p><p>You can get a <tt>RequestDispatcher</tt> object from either a request or the web
context; however, the two methods have slightly different behavior. The method takes the
path to the requested resource as an argument. A request can take a
relative path (that is, one that does not begin with a <tt>/</tt>), but
the web context requires an absolute path. If the resource is not available
or if the server has not implemented a <tt>RequestDispatcher</tt> object for that
type of resource, <tt>getRequestDispatcher</tt> will return null. Your servlet should be prepared to
deal with this condition.</p>

<a name="bnagj"></a><h4>Including Other Resources in the Response</h4>
<p><a name="indexterm-285"></a><a name="indexterm-286"></a>It is often useful to include another web resource (for example, banner content
or copyright information) in the response returned from a web component. To include
another resource, invoke the <tt>include</tt> method of a <tt>RequestDispatcher</tt> object:</p><pre>include(request, response);</pre><p>If the resource is static, the <tt>include</tt> method enables programmatic server-side includes. If
the resource is a web component, the effect of the method is to
send the request to the included web component, execute the web component, and
then include the result of the execution in the response from the containing
servlet. An included web component has access to the request object, but it
is limited in what it can do with the response object:</p>
<ul><li><p>It can write to the body of the response and commit a response.</p></li>
<li><p>It cannot set headers or call any method (for example, <tt>setCookie</tt>) that affects the headers of the response.</p></li></ul>
<p>The banner for the Duke&rsquo;s Bookstore application is generated by <tt>BannerServlet</tt>. Note
that both <tt>doGet</tt> and <tt>doPost</tt> are implemented because <tt>BannerServlet</tt> can be dispatched from
either method in a calling servlet.</p><pre>public class BannerServlet extends HttpServlet {
     public void doGet (HttpServletRequest request,
        HttpServletResponse response)
        throws ServletException, IOException {
            output(request, response);
    }
    public void doPost (HttpServletRequest request,
        HttpServletResponse response)
        throws ServletException, IOException {
            output(request, response);
}

private void output(HttpServletRequest request,
        HttpServletResponse response)
        throws ServletException, IOException {
        PrintWriter out = response.getWriter();
        out.println("&lt;body bgcolor=\"#ffffff\">" +
        "&lt;center>" + "&lt;hr> &lt;br> &amp;nbsp;" + "&lt;h1>" +
        "&lt;font size=\"+3\" color=\"#CC0066\">Duke&rsquo;s &lt;/font>" +
        &lt;img src=\"" + request.getContextPath() +
        "/duke.books.gif\">" +
         "&lt;font size=\"+3\" color=\"black\">Bookstore&lt;/font>" +
        "&lt;/h1>" + "&lt;/center>" + "&lt;br> &amp;nbsp; &lt;hr> &lt;br> ");
    }
}</pre><p>Each servlet in the Duke&rsquo;s Bookstore application includes the result from <tt>BannerServlet</tt> using
the following code:</p><pre>RequestDispatcher dispatcher =
    getServletContext().getRequestDispatcher("/banner");
if (dispatcher != null)
    dispatcher.include(request, response);
}</pre>

<a name="bnagk"></a><h4>Transferring Control to Another Web Component</h4>
<p>In some applications, you might want to have one web component do
preliminary processing of a request and have another component generate the response. For example,
you might want to partially process a request and then transfer to another
component depending on the nature of the request.</p><p><a name="indexterm-287"></a><a name="indexterm-288"></a>To transfer control to another web component, you invoke the <tt>forward</tt> method of
a <tt>RequestDispatcher</tt>. When a request is forwarded, the request URL is set to
the path of the forwarded page. The original URI and its constituent parts
are saved as request attributes <tt>javax.servlet.forward.[<i>request-uri</i>|<i>context-path</i>|<i>servlet-path</i>|<i>path-info</i>|<i>query-string</i>]</tt>. The <tt></tt><i>tut-install</i><tt>/javaeetutorial5/examples/web/bookstore2/src/java/com/sun/bookstore2/dispatcher/Dispatcher</tt> servlet, used by a
version of the Duke&rsquo;s Bookstore application described in <a href="bnall.html">The Example JSP Pages</a>, saves the path information from
the original URL, retrieves a <tt>RequestDispatcher</tt> from the request, and then forwards
to the JSP page, <tt></tt><i>tut-install</i><tt>/javaeetutorial5/examples/web/bookstore3/web/template/template.jsp</tt>.</p><pre>public class Dispatcher extends HttpServlet {
    public void doGet(HttpServletRequest request,
         HttpServletResponse response) {
        RequestDispatcher dispatcher = request.
            getRequestDispatcher("/template.jsp");
        if (dispatcher != null)
            dispatcher.forward(request, response);
    }
    public void doPost(HttpServletRequest request,
     ...
}</pre><p>The <tt>forward</tt> method should be used to give another resource responsibility for replying
to the user. If you have already accessed a <tt>ServletOutputStream</tt> or <tt>PrintWriter</tt> object within
the servlet, you cannot use this method; doing so throws an <tt>IllegalStateException</tt>.</p>
         </div>
         <div class="navigation">
             <a href="bnagb.html"><img style="padding-right: 3px" src="graphics/leftButton.gif" border="0"></a>
             <a href="sjsaseej2eet.html"><img style="padding-right: 3px" src="graphics/upButton.gif" border="0"></a>
             <a href="bnagl.html"><img style="padding-left: 3px" src="graphics/rightButton.gif" border="0"></a>
         </div>

         <div class="copyright">
      	    <p>The material in The Java&trade; EE 5 Tutorial is <a href='docinfo.html'>copyright</a>-protected and may not be published in other works without express written permission from Sun Microsystems.</p>
      	 </div>

      </td>
   </tr>
</tbody>
</table>
</body>
</html>

